package com.nand.module.function.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.lzy.okgo.model.HttpParams;
import com.nand.library.base.base.BaseFragment;
import com.nand.library.base.bean.ResponseBean;
import com.nand.library.base.http.MyCallback;
import com.nand.library.base.http.OkGoUtils;
import com.nand.library.base.utils.SpUtils;
import com.nand.module.function.R;
import com.nand.module.function.adapter.EpairListAdapter;
import com.nand.module.function.bean.EpairListBean;
import com.nand.module.function.constant.URL;
import com.nand.module.function.ui.EpairDetailsActivity;
import com.nand.module.function.utils.LogUtils;
import com.nand.module.function.utils.SpacesItemDecoration;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

/**
 * created by 86185
 * on 2020/3/3
 */
public class EpairAllListFragment extends BaseFragment {

    private RecyclerView mRecycler;
    private EpairListAdapter mRecyclerAdapter;
    private int type;
    private List<EpairListBean.DataListBean> mList = new ArrayList<>();
    private SmartRefreshLayout swipeRefres;
    private HttpParams paramsPost;
    private int page;//加载页数
    private LinearLayout llEmpty;
    private int userId;

    public static EpairAllListFragment newInstance(int page) {

        EpairAllListFragment fragment = new EpairAllListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("TYPE", page);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_epair_list;
    }

    @Override
    protected void initView(View view) {
        mRecycler = view.findViewById(R.id.recycler);
        type = getArguments().getInt("TYPE");
        swipeRefres = view.findViewById(R.id.swipe_refres);
        page = 1;
        userId = SpUtils.readInt(getActivity(), "userId", 0);
        llEmpty = view.findViewById(R.id.ll_empty);
        llEmpty.setVisibility(View.GONE);
        swipeRefres.setEnableRefresh(true);//是否启用下拉刷新功能
        swipeRefres.setEnableLoadMore(true);//是否启用上拉加载功能
        swipeRefres.setEnableAutoLoadMore(true);//是否启用列表惯性滑动到底部时自动加载更多
        swipeRefres.setRefreshHeader(new ClassicsHeader(getActivity()));
        swipeRefres.setRefreshFooter(new ClassicsFooter(getActivity()));

        swipeRefres.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                //刷新需要执行操作
                paramsPost = new HttpParams();
                paramsPost.put("current", String.valueOf(page));
                paramsPost.put("size", "10");
                initHttpData(false);
            }
        });
        swipeRefres.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                //刷新需要执行操作
                page++;
                paramsPost = new HttpParams();
                paramsPost.put("current", String.valueOf(page));
                paramsPost.put("size", "10");
                initHttpData(false);
            }
        });
        initData();
    }

    private void initData() {
        mRecycler.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerAdapter = new EpairListAdapter(getActivity(), 1);
        final int space = 20;
        mRecycler.addItemDecoration(new SpacesItemDecoration(space));
        mRecycler.setAdapter(mRecyclerAdapter);

        mRecyclerAdapter.setOnItemClickListener(new EpairListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                int state = mList.get(position).getState();
                String abnormalId = mList.get(position).getAbnormalId();
                Intent intent = null;
                Bundle bundle = null;
                switch (state){
                    case 0:  //待处理
                        intent = new Intent(getActivity(), EpairDetailsActivity.class);
                        bundle = new Bundle();
                        bundle.putInt("code", 102);
                        bundle.putString("abnormalId", abnormalId);
                        intent.putExtras(bundle);
                        break;
                    case 1:  //待确认
                        intent = new Intent(getActivity(), EpairDetailsActivity.class);
                        bundle = new Bundle();
                        bundle.putInt("code", 103);
                        bundle.putString("abnormalId", abnormalId);
                        intent.putExtras(bundle);
                        break;
                    case 3:  //待提交
                        intent = new Intent(getActivity(), EpairDetailsActivity.class);
                        bundle = new Bundle();
                        bundle.putInt("code", 104);
                        bundle.putString("abnormalId", abnormalId);
                        intent.putExtras(bundle);
                        break;
                    case 2:  //关闭
                    case 4: //终止
                        intent = new Intent(getActivity(), EpairDetailsActivity.class);
                        bundle = new Bundle();
                        bundle.putInt("code", 105);
                        bundle.putString("abnormalId", abnormalId);
                        intent.putExtras(bundle);
                        break;
                }
                if (null != intent){
                    startActivityForResult(intent,1001);
                }
                // startActivity(new Intent(getActivity(), EpairDetailsActivity.class));
            }
        });


        paramsPost = new HttpParams();
        paramsPost.put("current", "1");
        paramsPost.put("size", "10");
        initHttpData(true);
    }

    private void initHttpData(final boolean isFirst) {
        LogUtils.e("typeabcd====="+type);
        if (1 == type)
            paramsPost.put("userId", userId);

        OkGoUtils.getInstance().post(getActivity(), URL.EPAIR_LIST, paramsPost, 1, true, new MyCallback() {
            @Override
            public void onSuccess(ResponseBean response, int id) {
                if (200 == response.getCode()){
                    String s = response.getDataInfo().toString();
                    swipeRefres.finishLoadMore();

                    LogUtils.e("abcd====="+s);
                    LogUtils.e("abcd====="+isFirst);
                    LogUtils.e("pageabcd====="+page);
                    if (!isFirst)
                        swipeRefres.finishRefresh();


                    EpairListBean listBeans = JSON.parseObject(s, EpairListBean.class);
                    List<EpairListBean.DataListBean> data = listBeans.getDataList();
                    LogUtils.e("abcd====="+data.size());
                    if (page != 1) {
                        if (data == null || data.size() == 0) {
                            swipeRefres.finishLoadMoreWithNoMoreData();
                        }
                    } else {
                        if (data.size() == 0) {
                            llEmpty.setVisibility(View.VISIBLE);
                        } else {
                            llEmpty.setVisibility(View.GONE);
                        }
                        mList.clear();
                    }
                    for (EpairListBean.DataListBean bean : data) {
                        mList.add(bean);
                    }
                    mRecyclerAdapter.setData(mList);
                }else {
                    //  MsgUtil.toast(response.getMsg());
                }

            }

            @Override
            public void onError(Throwable e, int id) {

            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        LogUtils.e("fragmentfragmentabc====="+requestCode +"===="+resultCode);
        if (1001 == requestCode){
            paramsPost = new HttpParams();
            paramsPost.put("current", "1");
            paramsPost.put("size", "10");
            initHttpData(true);
        }

    }
}
